// Definition for singly-linked list.
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
    // 都不考虑释放
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
        // 记录长度
        int len = 1;
        ListNode *tail = head;
        while (tail->next)
        {
            tail = tail->next;
            len++;
        }
        // 长度为一
        if (len == 1)
            return nullptr;
        // 要删除头节点
        if (len == n)
            return head = head->next;
        // 找到要删除节点的前一个节点
        ListNode *cur = head;
        for (int i = 0; i < len - n - 1; i++)
        {
            cur = cur->next;
        }
        // 删除逻辑
        cur->next = cur->next->next;
        // delete head->next
        return head;
    }
};